/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.vwbe.impl.htmlunit;

import elaborate.vwbe.impl.*;
import elaborate.vwbe.VWBEContext;
import elaborate.vwbe.scripting.ScriptEngine;
import elaborate.vwbe.scripting.ScriptLanguage;
import elaborate.vwbe.scripting.ScriptingException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.w3c.dom.Document;

/**
 *
 * @author lendle
 */
public class HtmlUnitVWBEContext implements VWBEContext{

    protected Document hTMLDocument;
    protected Map<ScriptLanguage, ScriptEngine> scriptEngines=new ConcurrentHashMap<ScriptLanguage, ScriptEngine>();
    protected DefaultScriptEngineManager scriptEngineManager=new DefaultScriptEngineManager();
    protected URL baseURL;
    protected String charset;

    /**
     * Get the value of charset
     *
     * @return the value of charset
     */
    public String getCharset() {
        return charset;
    }

    /**
     * Set the value of charset
     *
     * @param charset new value of charset
     */
    public void setCharset(String charset) {
        this.charset = charset;
    }


    /**
     * Get the value of baseURL
     *
     * @return the value of baseURL
     */
    public URL getBaseURL() {
        return baseURL;
    }

    /**
     * Set the value of baseURL
     *
     * @param baseURL new value of baseURL
     */
    public void setBaseURL(URL baseURL) {
        this.baseURL = baseURL;
    }


    /**
     * Get the value of hTMLDocument
     *
     * @return the value of hTMLDocument
     */
    public Document getHTMLDocument() {
        return hTMLDocument;
    }

    /**
     * Set the value of hTMLDocument
     *
     * @param hTMLDocument new value of hTMLDocument
     */
    public void setHTMLDocument(Document hTMLDocument) {
        this.hTMLDocument = hTMLDocument;
        this.scriptEngineManager.setDocument(hTMLDocument);
    }

    public ScriptEngine getScriptEngine(ScriptLanguage language) throws ScriptingException{
        ScriptEngine engine=this.scriptEngines.get(language);
        if(engine==null){
            engine=this.scriptEngineManager.createScriptEngine(language);
            if(engine!=null){
                this.scriptEngines.put(language, engine);
            }
        }
        return engine;
    }

}
